/*
 * @lc app=leetcode.cn id=624 lang=cpp
 * @lcpr version=30204
 *
 * [624] 数组列表中的最大距离
 */


// @lcpr-template-start
using namespace std;
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// @lcpr-template-end
// @lc code=start
class Solution {
public:
    int maxDistance(vector<vector<int>>& arrays) {
        int ans = 0;
        int min_val = INT32_MAX, max_val = 0;
        
        min_val = arrays[0][0];
        max_val = *(arrays[0].end()-1);

        for (int i =1 ; i < arrays.size(); i++){
            ans = max(max(ans, max_val - arrays[i][0]),
            *(arrays[i].end()-1) - min_val);
            min_val = min(min_val, arrays[i][0]);
            max_val = max(max_val, *(arrays[i].end()-1));
        }


        return ans;
    }
};
// @lc code=end



/*
// @lcpr case=start
// [[1,2,3],[4,5],[1,2,3]]\n
// @lcpr case=end

// @lcpr case=start
// [[1],[1]]\n
// @lcpr case=end

 */

